<template>
	<view class="order-list">
		<cu-custom bgColor="bg-gradual-orange" :isBack="true">
			<block slot="content">会员订单</block>
		</cu-custom>
		<!-- 顶部导航 -->
		<!-- <view class="nav-bar">
			<view v-for="(item, index) in navItems" :key="index" :class="['nav-item', { active: currentTab === index }]"
				@tap="changeTab(index)">
				{{ item }}
			</view>
		</view> -->

		<!-- 订单列表 -->
		<!-- {{applyList}} -->
		<scroll-view scroll-y class="order-scroll">
			<view v-if="applyList.length === 0" class="empty-list">
				<image src="/static/empty-box.png" mode="aspectFit" class="empty-icon"></image>
				<text>暂无订单</text>
			</view>
			<view v-else class="order-item" v-for="(items, index) in applyList" :key="index">
				<view class="order-header">
					<text class="order-number">订单号: {{ items.order_sn }}</text>
					<text :class="['order-status', changeStatus(items)]">{{ changeText(items) }}</text>
				</view>
				<view class="order-content">
					<!-- <image :src="items.productImage" mode="aspectFill" class="product-image"></image> -->
					<view class="order-details">
						<text class="product-name">{{ items.pname }}</text>
						<text class="product-name" v-if="items.tracking_number">快递单号: {{ items.tracking_number }}</text>
						<text class="order-date">下单时间: {{ items.createtime | formDates }}</text>
					</view>
				</view>
				<view class="order-footer">
					<text class="order-amount">总价: ¥{{ items.pay_price.toFixed(2) }}</text>
					<button @tap="wuliu(items.tracking_number)" class="action-button pay-button" v-if="items.tracking_number">查看物流</button>
				</view>
				
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import banlance from '@/api/banlance';
	export default {
		data() {
			return {
				navItems: ['全部', '待支付', '待发货', '已完成'],
				currentTab: 0,
				applyList: []
			}
		},
		filters: {
			formDates(times) {
				const date = new Date(times * 1000);
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				const hour = String(date.getHours()).padStart(2, '0');
				const minute = String(date.getMinutes()).padStart(2, '0');
				const second = String(date.getSeconds()).padStart(2, '0');
				return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
			},
		},
		computed: {
			...mapState(['hasLogin', 'userInfo']),
			changeStatus() {
				return (items) => {
					switch (items.pay_status) {
						case 'unpaid':
							return 'bg-green';
						case 'paid':
							return 'bg-red';
						default:
							return 'bg-black';
					}
				};
			},
			changeText() {
				return (items) => {
					switch (items.pay_status) {
						case 'unpaid':
							return '未支付';
						case 'paid':
							return '已支付';
						default:
							return '未支付';
					}
				};
			},
		},
		onLoad(){
			this.changeTab(0)
		},
		methods: {
			changeTab(index) {
				this.currentTab = index;
				this.getRecord();
			},
			async getRecord(type) {
				const res = await banlance.getLevelOrder({
					page: this.page
				});
				if (res.code === 1) {
					this.applyList = this.applyList.concat(res.data);
					this.more = res.data.length ? 'more' : 'noMore';

					this.applyList = this.applyList.filter(order => {
						if (this.currentTab === 0) return true;
						return order.pay_status === this.navItems[this.currentTab];
					});
				}
			},
			wuliu(no) {
				if (!no) {
					uni.showToast({
						title: '尚未发货'
					})
					return
				}
				uni.navigateTo({
					url: '/pages/my/balance/wuliu?no=' + no
				})
			},
		},
		mounted() {
			// this.getOrderList();
		}
	}
</script>

<style lang="scss" scoped>
	.order-list {
		display: flex;
		flex-direction: column;
		height: 100vh;
		background-color: #f5f5f5;
	}

	.nav-bar {
		display: flex;
		justify-content: space-around;
		padding: 20rpx 0;
		background-color: #ffffff;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
	}

	.nav-item {
		font-size: 28rpx;
		color: #666;
		padding: 10rpx 20rpx;
		border-radius: 30rpx;
		transition: all 0.3s ease;

		&.active {
			color: #ffffff;
			background-color: #007aff;
			font-weight: bold;
		}
	}

	.order-scroll {
		// width: 95%;
		padding: 20rpx;
	}

	.order-item {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
	}

	.order-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.order-number {
		font-size: 26rpx;
		color: #666;
	}

	.order-status {
		font-size: 24rpx;
		padding: 6rpx 16rpx;
		border-radius: 20rpx;

		&.status-pending {
			background-color: #ffefd5;
			color: #ffa500;
		}

		&.status-processing {
			background-color: #e6f7ff;
			color: #1890ff;
		}

		&.status-completed {
			background-color: #f6ffed;
			color: #52c41a;
		}
	}

	.order-content {
		display: flex;
		margin-bottom: 20rpx;
	}

	.product-image {
		width: 160rpx;
		height: 160rpx;
		border-radius: 10rpx;
		margin-right: 20rpx;
	}

	.order-details {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.product-name {
		font-size: 28rpx;
		font-weight: bold;
		color: #333;
	}

	.order-date {
		font-size: 24rpx;
		color: #999;
	}

	.order-footer {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.order-amount {
		font-size: 32rpx;
		font-weight: bold;
		color: #ff6b6b;
	}

	.action-button {
		width:100px;
		font-size: 24rpx;
		padding: 5rpx 15rpx;
		border-radius: 30rpx;
		border: none;

		&.pay-button {
			background-color: #ff6b6b;
			color: #ffffff;
		}

		&.urge-button {
			background-color: #ffd166;
			color: #ffffff;
		}

		&.review-button {
			background-color: #4ecdc4;
			color: #ffffff;
		}
	}

	.empty-list {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100%;
		color: #999;

		.empty-icon {
			width: 200rpx;
			height: 200rpx;
			margin-bottom: 20rpx;
		}
	}
</style>